package rexsee.fingerdrawing;

import rexsee.core.browser.Browser;
import rexsee.core.lang.RexseeLanguage;
import rexsee.core.utilities.Utilities;
import rexsee.core.widget.DivDialog;
import rexsee.multimedia.RexseeImage;
import rexsee.natives.ViewStyle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FingerDrawingDialog extends DivDialog {

	public static final String EVENT_ONFINGERDRAWINGREADY = "onFingerDrawingReady";

	private final Context context;

	private final LinearLayout layout;
	private final FingerDrawingView fingerDrawing;

	public FingerDrawingDialog(final Browser browser, String style) {

		super(browser, null, "border-width:0;hint-color:#318D9D;line-height:9;icon-background-color:#ffffff;label:Finger Drawing;text-align:left;label-background-color:black;font-size:18px;label_padding:10;" + style);
		context = browser.getContext();

		TextView title = new TextView(context);
		ViewStyle.setTextViewStyle(browser, title, styleSheet);

		fingerDrawing = new FingerDrawingView(context);
		fingerDrawing.setLineWidth(Utilities.getInt(styleSheet.line_height, 5));
		fingerDrawing.setLineColor(Color.parseColor(styleSheet.hint_color));
		fingerDrawing.setBg(Color.parseColor(styleSheet.icon_background_color));

		FrameLayout container = new FrameLayout(context);
		container.setBackgroundColor(Color.GRAY);
		container.setPadding(2, 2, 2, 2);
		container.addView(fingerDrawing, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));
		FrameLayout content = new FrameLayout(context);
		content.setBackgroundColor(Color.TRANSPARENT);
		content.setPadding(15, 15, 15, 15);
		content.addView(container, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));

		Button buttonGoback = new Button(mContext);
		buttonGoback.setTextSize(14);
		buttonGoback.setText(RexseeLanguage.ACTION_BACK);
		buttonGoback.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fingerDrawing != null) fingerDrawing.goBack();
			}
		});

		Button buttonGoforward = new Button(mContext);
		buttonGoforward.setTextSize(14);
		buttonGoforward.setText(RexseeLanguage.ACTION_FORWARD);
		buttonGoforward.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fingerDrawing != null) fingerDrawing.goForward();
			}
		});

		Button buttonClear = new Button(mContext);
		buttonClear.setTextSize(14);
		buttonClear.setText(RexseeLanguage.ACTION_CLEAR);
		buttonClear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fingerDrawing != null) fingerDrawing.clear();
			}
		});

		Button buttonRecover = new Button(mContext);
		buttonRecover.setTextSize(14);
		buttonRecover.setText(RexseeLanguage.ACTION_RECOVER);
		buttonRecover.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fingerDrawing != null) fingerDrawing.recover();
			}
		});

		Button buttonConfirm = new Button(mContext);
		buttonConfirm.setTextSize(14);
		buttonConfirm.setText(mContext.getResources().getString(android.R.string.ok));
		buttonConfirm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (fingerDrawing == null) return;
				String path = styleSheet.path;
				if (path == null || !path.startsWith("file://")) path = "file:///sdcard/fingerdrawing_" + System.currentTimeMillis() + ".jpg";
				RexseeImage.saveBitmap(fingerDrawing.getBitmap(), 90, path);
				dismiss();
				browser.eventList.run(EVENT_ONFINGERDRAWINGREADY, new String[]{path});
			}
		});

		LinearLayout buttonLayout = new LinearLayout(mContext);
		buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
		buttonLayout.setBackgroundColor(Color.parseColor(styleSheet.label_background_color));
		int padding1 = (int) (mBrowser.application.screenDensity * 8);
		buttonLayout.setPadding(0, padding1, 0, 0);
		int buttonHeight = (int) (mBrowser.application.screenDensity * 40);
		buttonLayout.addView(buttonGoback, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, buttonHeight, 1));
		buttonLayout.addView(buttonGoforward, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, buttonHeight, 1));
		buttonLayout.addView(buttonClear, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, buttonHeight, 1));
		buttonLayout.addView(buttonRecover, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, buttonHeight, 1));
		buttonLayout.addView(buttonConfirm, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, buttonHeight, 1));

		layout = new LinearLayout(context);
		layout.setBackgroundColor(Color.TRANSPARENT);
		layout.setOrientation(LinearLayout.VERTICAL);

		layout.addView(title, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		layout.addView(content, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, 1));
		layout.addView(buttonLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		this.addChild(layout);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mBrowser.menu.isOptionsMenuOpen) {
				((Activity) mContext).closeOptionsMenu();
				return true;
			} else {
				if (styleSheet.window_cancelable.equalsIgnoreCase("true")) dismiss();
			}
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			((Activity) mContext).openOptionsMenu();
		} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_FOCUS) {
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
		} else {
		}
		return false;
	}

}
